package com.welkom.everydate.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.welkom.everydate.domain.Role;

@Repository
public class RoleDao extends BaseDao{
	public static final String GET_ROLES_BY_USER_ID="getRolesByUserId";
	public static final String GET_ROLE_BY_ROLE_ID="getRoleByRoleId";
	public static final String GET_ALL_ROLES="getAllRoles";
	public static final String GET_TOTAL_COUNT_BY_CONDITION="getRoleTotalCountByCondition";
	public static final String GET_ROLES_BY_CONDITION="queryRoleByCondition";
	public static final String UPDATE_ROLE="updateRole";
	public static final String INSERT_ROLE="insertRole";
	public static final String DELETE_ROLE_BY_ID="deleteRoleById";
	public static final String REMOVE_USER_ROLE_RELATIONS_BY_USER_ID="removeUserRoleRelationsByUserId";
	public static final String INSERT_USER_ROLE_RELATIONS="insertUserRoleRelations";
	public static final String INSERT_USER_ROLE_RELATION="insertUserRoleRelation";
	@Resource
	private AuthorityDao authorityDao;
	public List<Role> getRolesByUserId(long userId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("user_id", userId);
		List<Role> roles =(List<Role>)getSqlMapClientTemplate().queryForList(GET_ROLES_BY_USER_ID, parameters);
		for (Role role:roles){
			role.setAuthorities(authorityDao.getAuthoritiesByRoleId(role.getRoleId()));
		}
		return roles;
	}
	
	
	public Role getRoleByRoleId(long roleId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("role_id", roleId);
		Role role=(Role)getSqlMapClientTemplate().queryForObject(GET_ROLE_BY_ROLE_ID, parameters);
		if(role!=null){
		role.setAuthorities(authorityDao.getAuthoritiesByRoleId(roleId));
		}
		return role;
	}
	
	
	public List<Role> getAllRoles(){
		List<Role> roles =(List<Role>)getSqlMapClientTemplate().queryForList(GET_ALL_ROLES);
		for (Role role:roles){
			role.setAuthorities(authorityDao.getAuthoritiesByRoleId(role.getRoleId()));
		}
		return roles;
	}
	/**
	 * 根据条件查询总数量
	 * @param role
	 * @return
	 */
	public int getRoleTotalCountByCondition(Role role){
		return (Integer)getSqlMapClientTemplate().queryForObject(GET_TOTAL_COUNT_BY_CONDITION,role);
	}
	/**
	 * 根据条件和分页查询角色列表
	 * @param role
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	public List<Role> queryRolesByCondtion(Role role,int startIndex,int pageSize){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("roleName", role.getRoleName());
		parameters.put("roleEnabled", role.getRoleEnabled());
		parameters.put("defaultRoleUrl", role.getDefaultRoleUrl());
		parameters.put("startIndex", startIndex);
		parameters.put("pageSize", pageSize);
		return (List<Role>)getSqlMapClientTemplate().queryForList(GET_ROLES_BY_CONDITION, parameters);
	}
	/**
	 * 更新角色
	 * @param role
	 */
	public void updateRole(Role role){
		getSqlMapClientTemplate().update(UPDATE_ROLE,role);
	}
	/**
	 * 增加角色
	 * @param role
	 */
	public void addRole(Role role){
		getSqlMapClientTemplate().insert(INSERT_ROLE, role);
	}
	/**
	 * 删除角色
	 */
	public void deleteRoleById(long roleId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("role_id", roleId);
		getSqlMapClientTemplate().delete(DELETE_ROLE_BY_ID, parameters);
	}
	/**
	 * 删除用户的所有的角色
	 * @param userId
	 */
	public void removeUserRoleRelationsByUserId(long userId){
		Map<String,Object> parameters=new HashMap<String,Object>();
		parameters.put("user_id", userId);
		getSqlMapClientTemplate().delete(REMOVE_USER_ROLE_RELATIONS_BY_USER_ID, parameters);
	}
	/**
	 * 添加用户角色
	 * @param roles
	 * @param userId
	 */
	public void insertUserRoleRelations(List<Role> roles,long userId){
		for(Role role:roles){
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("user_id", userId);
			map.put("role_id", role.getRoleId());
			getSqlMapClientTemplate().insert(INSERT_USER_ROLE_RELATION, map);
		}
	}
}
